6 bool g
[200][200]; //matriz de adyacencia del grafo
9 bool visiteEdge(const int &u
, const int &v
, vector
<int> visitas
){
10 for (int i
=0; i
<visitas
.size()-1; i
++){
11 if ((visitas
[i
] == u
&& visitas
[i
+1] == v
) || (visitas
[i
] == v
&& visitas
[i
+1] == u
))
17 void bt(int nodo
, const int &n
, vector
<int> visitados
){
18 if (!bicolorable
) return;
21 for (int i
=0; i
<n
; ++i
){
22 if (g
[nodo
][i
]) vecinos
.push_back(i
);
24 //backtrack for each neighbour
25 for (int i
=0; i
<vecinos
.size(); ++i
){
26 if (!visiteEdge(nodo
, vecinos
[i
], visitados
)){
28 for (int j
=0; j
<visitados
.size(); ++j
){
29 if (visitados
[j
] == vecinos
[i
]) k
= j
;
31 if (k
> -1 && ((visitados
.size() - k
) % 2) == 1){
32 /*it means I have already visited node vecinos[i], which is at position k
33 of visitados, i.e, vecinos[i] = visitados[k]. It also means that I traveled
34 an odd number of nodes to arrive to vecinos[i] again, which means the graph
35 has an odd cycle and thus is not bicolorable. */
38 vector
<int> temp(visitados
);
39 temp
.push_back(vecinos
[i
]);
40 e
[nodo
][vecinos
[i
]] = e
[vecinos
[i
]][nodo
] = true;
41 bt(vecinos
[i
], n
, temp
);
52 memset(g
, 0, sizeof(g
));
57 for (int i
=0; i
<l
; ++i
){
60 g
[x
][y
] = g
[y
][x
] = true;
63 for (int i
=0; i
<n
; ++i
){
64 //bt starting at every node
66 vector
<int> visitados
;
67 visitados
.push_back(i
);
71 if (!bicolorable
) cout
<< "NOT BICOLORABLE.\n";
72 else cout
<< "BICOLORABLE.\n";